local PartnerSkillLevelUpPop = class("PartnerSkillLevelUpPop", app.mvc.ViewBase)
PartnerSkillLevelUpPop.VIEW_ROOT_RES = Res.PartnerSkillLevelUp

local Item = require("app.public.dao.Item")
local IconRender = require("app.widget.renders.IconRender")
local Daibi = require("app.public.dao.Daibi")
function PartnerSkillLevelUpPop:init()
    display.setCascadeOpacityEnabled(self._root, true)
    self._ui.bg:setTouchEnabled(true)
    display.uiAddClick(self._ui.bg, function()
        self:closeView()
    end)
    self._ui.Button_close:addClickEventListener(function()
        self:closeView()
    end)
    self._ui.Button_up:addClickEventListener(handler(self, self._onSkillLevelUp))
    
    self:registerViewEvent({
        {name = ViewEvent.Partner_DataChange, callback = handler(self, self._onPartnerDataChange)},
        {name = ViewEvent.Partner_SkillLevelUpResult, callback = handler(self, self._onPartnerLevelUpResult)},
        
    })

    self._icon1 = IconRender.create(self._ui["Panel/Panel_content/Panel_Icons/icon_1"])
    self._icon2 = IconRender.create(self._ui["Panel/Panel_content/Panel_Icons/icon_2"])
    self._icon3 = IconRender.create(self._ui["Panel/Panel_content/Panel_Icons/icon_3"])
    for i =1, 3  do
        self["_icon"..i]:setTipEnable(true)
        self["_icon"..i]:setTipsCloseCallBack(function(tp)
            if tp == Enums.TIPS_HIDE.GOTO then
                executeFunc(self._onCloseView, self)
            end
        end)
    end

    self._iconPanelWidth = self._ui["Panel/Panel_content/Panel_Icons"]:getContentSize().width

    self._ui["Panel/Panel_content/Panel_skill_1/ScrollView/text"]:setTextAreaSize(cc.size(320, 0))
    self._ui["Panel/Panel_content/Panel_skill_2/ScrollView/text"]:setTextAreaSize(cc.size(320, 0))
    self._ui["Panel/Panel_content/Panel_skill_1/ScrollView/text"]:setAnchorPoint(cc.p(0, 1))
    self._ui["Panel/Panel_content/Panel_skill_2/ScrollView/text"]:setAnchorPoint(cc.p(0, 1))

    self._skillScrollViewHeight1 = self._ui["Panel/Panel_content/Panel_skill_1/ScrollView"]:getContentSize().height
    self._skillScrollViewHeight2 = self._ui["Panel/Panel_content/Panel_skill_2/ScrollView"]:getContentSize().height
    self:refresh()
end


function PartnerSkillLevelUpPop:_onSkillLevelUp()
    if not self._data then
        return
    end
    local canUpgrade, errCode = self._data:checkCanUpgrade()
    if not canUpgrade then
        if errCode == 3 then
            display.pushToast(L("partnerSkillItemNotEnough"))
        end
        return
    end
    UD.partnersData:C2S_EnhancePartnerSkill(self._data:getId())
end

function PartnerSkillLevelUpPop:refresh()
    if not self._data then
        return
    end

    local visibleItems = {}
    local itemDatas = self._data:getUpgradeCost()
    for i =1, 3  do
        local icon = self["_icon"..i]
        local icondata = itemDatas[i]
        if icondata then
            icon:setVisible(true)
            local itemTemp
            local maxNum = 0
            if icondata.id == 0 then
                maxNum = UD:getDaibiValue(icondata.type)
                itemTemp = Daibi.new(Enums.ItemBagType.Daibi)
                itemTemp:read({tid = icondata.type})
            else
                maxNum = UD:getItemAmountByTid(icondata.id)
                itemTemp = Item.new(icondata.type)
                itemTemp:read({tid = icondata.id})
            end
            icon:setData(itemTemp)
            icon:setNumString(string.format("%s", icondata.num))
            if maxNum >= icondata.num then
                icon:setNumberColor(display.COLOR_GREEN)
            else
                icon:setNumberColor(display.COLOR_RED)
            end
            table.insert(visibleItems, icon)
        else
            icon:setVisible(false)
        end
    end
    display.autoAdjustNodesPosX(self._iconPanelWidth, visibleItems, #visibleItems, 20)

    local canUgrade = self._data:checkCanUpgrade()
    -- self._ui.Button_up:setTouchEnabled(canUgrade)
    self._ui.Button_up:setBright(canUgrade)


    self._ui["Panel/Panel_content/Panel_skill_1/title"]:setString(string.format(L("PartnerSkillLevelUpTitle"),self._data:getName(), self._data:getLevel(), self._data:getSkillCd()/1000))
    self._ui["Panel/Panel_content/Panel_skill_1/ScrollView/text"]:setString(self._data:getDesc())
    local text1Size = self._ui["Panel/Panel_content/Panel_skill_1/ScrollView/text"]:getContentSize()
    if text1Size.height < self._skillScrollViewHeight1 then
        text1Size.height = self._skillScrollViewHeight1
    end
    self._ui["Panel/Panel_content/Panel_skill_1/ScrollView"]:setInnerContainerSize(text1Size)
    self._ui["Panel/Panel_content/Panel_skill_1/ScrollView/text"]:setPosition(cc.p(0, text1Size.height))

    if self._data:isMaxLevel() then
        self._ui["Panel/Panel_content/Panel_skill_2"]:setVisible(false)
    else
        self._ui["Panel/Panel_content/Panel_skill_2"]:setVisible(true)
        self._ui["Panel/Panel_content/Panel_skill_2/ScrollView/text"]:setString(self._data:getNextLevelDesc())
        self._ui["Panel/Panel_content/Panel_skill_2/title"]:setString(string.format(L("PartnerSkillLevelUpTitle"),self._data:getName(), self._data:getLevel() + 1, self._data:getNextSkillCd()/1000))
        local text2Size = self._ui["Panel/Panel_content/Panel_skill_2/ScrollView/text"]:getContentSize()
        if text2Size.height < self._skillScrollViewHeight2 then
            text2Size.height = self._skillScrollViewHeight2
        end
        self._ui["Panel/Panel_content/Panel_skill_2/ScrollView"]:setInnerContainerSize(text2Size)
        self._ui["Panel/Panel_content/Panel_skill_2/ScrollView/text"]:setPosition(cc.p(0, text2Size.height))
    end
end

function PartnerSkillLevelUpPop:_onPartnerDataChange()
    if not self._data then
        return
    end
    if self._data:isMaxLevel() then
        EM:notify(ViewEvent.Partner_SkillLevelUpMax, self._data)
        executeFunc(self._onCloseView, self)
    else
        self:refresh()
    end
end

function PartnerSkillLevelUpPop:_onPartnerLevelUpResult(eventName, ret)
    if ret then
        display.pushToast(L("tips_1070002"))
    else
        display.pushToast(L("tips_1070003"))
    end

end



return PartnerSkillLevelUpPop